<?php
class Zendvn_Template{
	
	private $_templatePath;
	
	private $_fileConfig = 'template.ini';
	
	private $_sectionConfig = 'default';
	
	private $_zAction;
	
	/*
	 * $options['templatePath'] path to template folder
	 * $options['fileConfig'] file config .ini
	 * $options['sectionConfig'] section config in INI file
	 * 
	 */
	public function __construct(Zend_Controller_Action $zAction, $options = null){
		$this->_zAction = $zAction;
	}
	
	public function load(){
		$zAction = $this->_zAction;
		
		$zAction->view->headScript()->getContainer()->exchangeArray(array());
		$zAction->view->headMeta()->getContainer()->exchangeArray(array());
		$zAction->view->headLink()->getContainer()->exchangeArray(array());
		
		$filename = $this->_templatePath . '/' . $this->_fileConfig;
		$config  = new Zend_Config_Ini($filename,$this->_sectionConfig);
		$arrConfig = $config->toArray();
		
		//========================= Icon =========================
		if(!empty($arrConfig['favIcon'])){
			$favIconUrl = TEMPLATE_URL . '/public/default/' . $arrConfig['favIcon'];
			$zAction->view->headLink(array('rel' => 'Shortcut Icon',
										'href' => $favIconUrl,
										'type' => 'image/x-icon'
									));
		}
		
		
		$zAction->view->headTitle($arrConfig['title'],true);
		$metaHttp = $arrConfig['metaHttp'];
		if(count($metaHttp)>0){
			foreach ($metaHttp as $val){
				$tmp = explode('||', $val);
				$zAction->view->headMeta()->appendHttpEquiv($tmp[0],$tmp[1]);
			}
		}
		
		$metaName = $arrConfig['metaName'];
		if(count($metaName)>0){
			foreach ($metaName as $val){
				$tmp = explode('||', $val);
				$zAction->view->headMeta()->appendName($tmp[0],$tmp[1]);
			}
		}
		
		$cssUrl = $arrConfig['url'] . $arrConfig['dirCss'];
		$fileCss = $arrConfig['fileCss'];
		if(count($fileCss)>0){
			foreach ($fileCss as $val){				
				$zAction->view->headLink()->appendStylesheet($cssUrl . $val,'screen' );
			}
		}
		
		$jsUrl = $arrConfig['url'] . $arrConfig['dirJs'];
		$fileJs = $arrConfig['fileJs'];
		if(count($fileJs)>0){
			foreach ($fileJs as $val){				
				$zAction->view->headScript()->appendFile($jsUrl . $val);
			}
		}
		
		if(count($arrConfig['dirs'])>0){
			foreach ($arrConfig['dirs'] as $val){
				$arrDir[$val] = $arrConfig['url']  . '/' . $val;
			}
		}
		
		$options = array(
						'layout'=>$arrConfig['layout'],
						'layoutPath'=>$this->_templatePath,						
						'viewSuffix'=>'phtml',						
						);
		Zend_Layout::startMvc($options);
		$zAction->view->arrDir = $arrDir;
		
		
	}
	
	public function setPath($value = null){
		$this->_templatePath = $value;
	}
	
	public function getPath(){
		return $this->_templatePath;
	}
	
	public function setFileConfig($value = 'template.ini'){
		$this->_fileConfig = $value;
	}
	
	public function getFileConfig(){
		return $this->_fileConfig;
	}
	
	public function setSectionConfig($value = 'default'){
		$this->_sectionConfig = $value;
	}
	
	public function getSectionConfig(){
		return $this->_sectionConfig;
	}
	
}